const Video = require("Video");
const Acceleration = require("Acceleration");
cc.Class({
    extends: cc.Component,

    properties: {
        // 
        spritePlanet: {
            default: null,
            type: cc.Node
        },
        // 
        targetLayer: {
            default: null,
            type: cc.Node
        },
        // 
        spriteCrosshair: {
            default: null,
            type: cc.Node
        },
        // 
        spriteBoom: {
            default: null,
            type: cc.Node
        },
        lableLayer: {
            default: null,
            type: cc.Node
        },
        lableX: {
            default: null,
            type: cc.Label
        },
        lableY: {
            default: null,
            type: cc.Label
        },
        ac: {
            default: null,
            type: Acceleration
        }
    },

    // use this for initialization
    onLoad: function () {
        this.ac = new Acceleration()
        this.spritePlanet.getComponent("Ball").ballScene = this;
        this.ac.addListener(this.accelerationListener, this);
    },

    accelerationListener: function (ax, ay, az) {


        var sx = ax / 30;
        var sy = (ay + 5) / 30;
        var halfw = 640 / 3;//this.spriteTarget.width / 3;
        var halfh = 960 / 3;//this.spriteTarget.height / 3;
        var pos = this.spriteCrosshair.getPosition();
        var x = Math.min(pos.x + sx, halfw);
        x = Math.max(x, -halfw);
        var y = Math.min(pos.y + sy, halfh);
        y = Math.max(y, -halfh);
        this.spriteCrosshair.setPosition(x, y);

        var dis = cc.pDistance(this.spriteCrosshair.getPosition(), cc.p(0, 0));
        this.lableX.string = Math.abs(Math.floor(x));
        this.lableY.string = Math.abs(Math.floor(y));
        if (dis < 10) {
            this.spriteCrosshair.setPosition(0, 0);
            this.lableX.string = 0;
            this.lableY.string = 0;
            this.playHit();
            this.ac.removeListener();
        }
    },

    playHit: function () {
        var anim = this.spriteCrosshair.getComponent(cc.Animation);
        anim.play('target_hit');
        this.scheduleOnce(this.playBoom, 1);
    },

    playBoom: function () {
        this.spriteCrosshair.active = false;
        this.spriteBoom.active = true;
        var anim = this.spriteBoom.getComponent(cc.Animation);
        anim.play('target_boom');
        this.spritePlanet.active = false;
        this.targetLayer.active = false;
        this.lableLayer.active = false;
    },

    onAnimCompleted: function (string) {
        alert("com  " + string);
        if (string == "target_hit") {
            //   this.playBoom();
        }
        // if (string == "ballmove") {
        //     this.spriteBoom.active = true;
        //     var anim = this.spriteBoom.getComponent(cc.Animation);
        //     anim.play('boom');
        //     this.spritePlanet.active = false;
        //     this.targetLayer.active = false;
        // }

        //console.log('onAnimCompleted:  param2[%s]', string);
    }

});
